package com.aeric.qrpro;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.aeric.CWUtility.QRCodeUtil;
import com.aeric.CWDomain.GlobalVariables;


public class CreatQRActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_qr);

        final EditText contentET = (EditText) findViewById(R.id.create_qr_content);

        final ImageView imageView = (ImageView) findViewById(R.id.create_qr_iv);

        final CheckBox addLogoCB = (CheckBox) findViewById(R.id.create_qr_addLogo);
        Button createQrBtn = (Button) findViewById(R.id.create_qr_btn);


        createQrBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String filePath = GlobalVariables.PHOTO_FILE_PATH + "qr_" + System.currentTimeMillis() + ".jpg";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        QRCodeUtil qRCodeUtil = new QRCodeUtil();
                        qRCodeUtil.setScale(3);
                        boolean success = qRCodeUtil.createQRImage(contentET.getText().toString().trim(), 400, 400,
                                addLogoCB.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) : null,
                                filePath);
                        if (success) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
                                }
                            });
                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    imageView.setImageResource(R.drawable.empty_photo);
                                }
                            });

                        }
                    }
                }).start();

            }
        });
    }
}